import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ReadBinaryTest {
 public String testDataInputStream(String filepath) throws IOException {
  File file = new File(filepath);
  DataInputStream din = new DataInputStream(new FileInputStream(file));
  StringBuilder hexData = new StringBuilder();
  byte temp = 0;
  int index=1;
  for(int i=0;i<file.length();i++) {
	   temp = din.readByte();
	   // 以十六进制的无符号整数形式返回一个字符串表示形式。
	   String str = Integer.toHexString(temp);
	   if(str.length() == 8) {//去掉补位的f
	    str = str.substring(6);
	   }
	   if(str.length() == 1) {
	    str = "0"+str;
	   }
	   
	   if(index==1){
		   hexData.append(" db ");
	   }
	   
	   
	   if(index%16==0){
		   hexData.append("0x"+ str.toUpperCase()+" ;  "+i);
		   hexData.append("\r\n db ");
	   }else{
		   hexData.append("0x"+ str.toUpperCase()+",");
	   }
	   index ++;
	  
  }
  return hexData.toString();
 }
 public static void main(String[] args) throws IOException {
  String filepath = "E:/os/myos/code/hankaku1.bin";
  ReadBinaryTest test = new ReadBinaryTest();
  String s = test.testDataInputStream(filepath);

  FileOutputStream fout = new FileOutputStream("E:\\b.txt");//将十六进制数存成文本
  fout.write(s.getBytes());
  fout.close();
 }
}